﻿var LoaderObject = function () {
    /*
        The Object that manages the position of the loader and whether or not the loader GIF is showing or not.
    */

    var This = this
		, root = $('#Loader')
		, Periods = $('#Periods')
		, halfWidth = 0 //the width of the loader
		, halfHeight = 0 //the height of the loader
		, timer = null
    ;

    function _init() {
        halfWidth = root.width() / 2;
        halfHeight = root.height() / 2;
        root.hide();
    }

    this.show = function () {
        /*
            Displays the Loader with a 500ms delay.
        */

        if (typeof timer == 'number') {
            clearTimeout(timer);
        }

        timer = setTimeout(function () {
            var pos = Utility.Dom.pos(Periods[0]);

            root.stop(true).css({
                position: 'absolute'
				, top: (pos.y + (pos.height / 2) - halfHeight)
				, left: (pos.x + (pos.width / 2) - halfWidth)
            }).fadeIn('slow');

        }, 500);
    };

    this.hide = function () {
        /*
            Hides the loader from view, or stops the timer if needed.
        */
        if (typeof timer == 'number') {
            clearTimeout(timer);
        }
        root.stop(true).fadeOut();
    }

    _init();
};